<template>
  <div>
    <h1>基本设置</h1>
    <el-container>
      <el-aside width=50%>
        <el-form ref="form" :model="form" label-width="80px">
          <div>
            <p class="text">用户类型</p>
            <el-input  v-model="input1" :disabled="true" class="input_text">
            </el-input>
          </div>
          <div>
            <p class="text">账号</p>
            <el-input  v-model="input2" :disabled="true" class="input_text">
            </el-input>
          </div>

          <div>
            <p class="text"><span class="text_red">*</span>姓名</p>
            <el-input v-model="input3" class="input_text"></el-input>
          </div>
          <div>
            <p class="text"><span class="text_red">*</span>手机号</p>
            <el-input v-model="input4" class="input_text"></el-input>
          </div>
          <div>
            <p class="text"><span class="text_red">*</span>邮箱</p>
            <el-input v-model="input5" class="input_text"></el-input>
          </div>
          <div>
            <el-button type="primary" @click="onSubmit" id="btn_mod">确认修改</el-button>
          </div>

        </el-form>

      </el-aside>

      <el-main>
        <div>
          头像<el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
           >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <el-button class="btn" @click="mod"><i class="el-icon-upload2"></i> 更换头像</el-button>
          
          </el-upload>
         
        </div>
      </el-main>
    </el-container>
  </div>

</template>
<script>
  export default {
    data() {
      return {
        input1:'',
        input2:'',
        input3:'',
        input4:'',
        input5:'',
        imageUrl: '',
        form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: '',
          
        tableData: {},
        arr1:[],
        fileList:[{name:'',url:''}]
        }
      }
    },
    methods: {
      onSubmit() {
        this.tableData.num=this.input1;
        this.tableData.userId=this.input2;
        this.tableData.userName=this.input3;
        this.tableData.tel=this.input4;
        this.tableData.email=this.input5;
        console.log(this.tableData);
        let list =[]
        let id = this.tableData['num']
        console.log(id);
        for (const key in this.tableData) {
          list.push(key)
          list.push(this.tableData[key])
        }
        console.log(this.tableData['num']);
        console.log(list);
         this.$axios({
           url:'/user/edit',
           method: 'post',
          data: {
            data:list,
            userid:id
          }
         }).then((res) => {
           console.log(res);
            sessionStorage.setItem('user',JSON.stringify(this.tableData));
         })
      },
      mod(){
        console.log('1');
 
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      getuser: function () {
        this.tableData=JSON.parse(sessionStorage.getItem('user'));
        this.input1=this.tableData.num;
        this.input2=this.tableData.userId;
        this.input3=this.tableData.userName;
        this.input4=this.tableData.tel;
        this.input5=this.tableData.email;

      }
    },
    created() {
      this.getuser();
    },
  }
</script>

<style lang="less" scoped>
  .el-aside,
     h1 {
    margin-left: 50px;
    font-size: 26px;
  }

  .el-aside>.el-form .el-input {
    width: 65%;
  }

  .el-main {
    color: #333;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .text {
    margin: 10px 0;
  }

  .text_red {
    color: red;
  }

  .input_text {
    margin-bottom: 15px;
  }

  #btn_mod {
    margin-left: 24%;
  }
  .btn{
    display: block;
  }
</style>